package com.edge.pbx.view.common;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.edge.pbx.view.summing.PbxOutputSelectChannelsLayout;

import com.edge.pbxdsp.R;
import com.qztech.utils.YzDebug;

/**
 * Copyright (C), 深圳市东乘电子科技有限公司
 * Carate By YangZhong
 */
public class ChNameRadioView extends View {
    private Drawable mDrawable, mDrawableS;
    private String mNameTop = "CHqfjp", mNameBottom = "ABCDEF";
    private Paint mPaint;
    private int mTextMaxHeight;
    private int mCenterX;
    @RequiresApi(api = Build.VERSION_CODES.M)
    public ChNameRadioView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setFakeBoldText(true);
        mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mDrawable = context.getResources().getDrawable(R.drawable.ch_and_n, context.getTheme());
        mDrawableS = context.getResources().getDrawable(R.drawable.pbx_common_ch_name_and_s, context.getTheme());
    }

//
//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        switch (event.getAction()){
//            case MotionEvent.ACTION_DOWN:
//                setPressed(true);
//                break;
//            case MotionEvent.ACTION_CANCEL:
//            case MotionEvent.ACTION_UP:
//                setPressed(false);
//                break;
//        }
//        return super.onTouchEvent(event);
//    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        final int width = right - left;
        final int height = bottom - top;
        mCenterX = width >> 1;
        float drawablePresent = 26f / 172f;
        int drawableH = (int) (height * drawablePresent);//0.15
        int drawableW = (int) (drawableH * 62f / 26);
        int drawableL = (width - drawableW) / 2;
        int drawableT = (height - drawableH) / 2;

        mTextMaxHeight = (height - drawableH) / 2;
        mDrawable.setBounds(drawableL, drawableT, drawableL + drawableW, drawableT + drawableH);
        mDrawableS.setBounds(drawableL, drawableT, drawableL + drawableW, drawableT + drawableH);
        mPaint.setTextSize(height * (1f - drawablePresent) * 0.35f);
        calTopNameLocation();
        calBottomNameLocation();
    }

    private void calTopNameLocation() {
        Paint.FontMetrics metrics = mPaint.getFontMetrics();
//        mTopNameY = (int) (mTextMaxHeight / 2 - metrics.descent + (metrics.bottom - metrics.top) / 2);
        mTopNameY = (int) ((mTextMaxHeight/2 - (metrics.descent +metrics.ascent)/2)*1.05f);
    }

    private void calBottomNameLocation() {
        Paint.FontMetrics metrics = mPaint.getFontMetrics();
//        mBottomNameY = getHeight() - mTextMaxHeight + (int) (mTextMaxHeight / 2 - metrics.descent + (metrics.bottom - metrics.top) / 2);
        mBottomNameY = (int) (getHeight() - mTextMaxHeight/2 - (metrics.descent +metrics.ascent)/2);
    }

    private int mTopNameY;

    public void setTopName(String name) {
        mNameTop = name;
        calTopNameLocation();
    }

    private int mBottomNameY;

    public void setBottomName(String name) {
        mNameBottom = name;
        calBottomNameLocation();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (isSelected() || isPressed()) {
            mPaint.setColor(PbxOutputSelectChannelsLayout.sColorSelected);
            mDrawableS.draw(canvas);
        } else {
            mPaint.setColor(Color.WHITE);
            mDrawable.draw(canvas);
        }

        if (mNameTop != null) {
            canvas.drawText(mNameTop, mCenterX, mTopNameY, mPaint);
        }
        if (mNameBottom != null) {
            canvas.drawText(mNameBottom, mCenterX, mBottomNameY, mPaint);
        }
//        canvas.drawColor(Color.RED, PorterDuff.Mode.MULTIPLY);

    }
}
